import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../../../../bean/CustomerList.dart';
import '../../../../config/RouteConfig.dart';
import '../../../../config/StyleConfig.dart';
import '../../../../res/HDColors.dart';
import '../../../../util/HDNavigator.dart';
import '../../../../view/common_widget.dart';
import '../../../../view/empty_data_widget.dart';
import 'logic.dart';

class UnNetPage extends StatelessWidget {
  UnNetPage({Key? key}) : super(key: key);

  final logic = Get.put(UnNetLogic());
  final state = Get.find<UnNetLogic>().state;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          logic.closePage();
          return false;
        },
        child: Scaffold(
          appBar: AppBar(
              title: const Text("待并网"),
              elevation: 0,
              backgroundColor: HDColors.green,
              actions: [
                GetBuilder<UnNetLogic>(builder: (logic) {
                  return Visibility(
                      visible: false,
                      child: TextButton(
                          onPressed: () {},
                          child: const Text("操作记录",
                              style: TextStyle(
                                  color: HDColors.white,
                                  fontSize: StyleConfig.fontContentSize14))));
                }),
              ],
              leading: CommonWidget.getAppBarLeading()),
          body: GetBuilder<UnNetLogic>(builder: (logic) {
            return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: CommonWidget.searchTitle(
                            state.searchController, (value) => {}, [], "",
                            searchHintText: "请输入手机号/客户姓名",
                            onSubmitted: (value) {
                          logic.requestList(true, searchKey: value);
                        }),
                      )
                    ],
                  ),
                  CommonWidget.divider(0.5),
                  SizedBox(
                    height: 10,
                  ),
                  Expanded(child: buildList())
                ]);
          }),
        ));
  }

  /// 数据列表
  buildList() {
    return GetBuilder<UnNetLogic>(builder: (logic) {
      return EasyRefresh(
          enableControlFinishRefresh: true,
          enableControlFinishLoad: true,
          controller: state.controller,
          emptyWidget: state.dataList.isEmpty ? const EmptyDataWidget() : null,
          onRefresh: () async {
            state.noMore = true;
            state.controller.finishRefresh();
            logic.requestList(true);
          },
          onLoad: state.noMore
              ? null
              : () async {
                  logic.requestList(false);
                },
          child: ListView.separated(
            shrinkWrap: true,
            itemCount: state.dataList.length,
            itemBuilder: (BuildContext context, int index) {
              return buildProfileItem(index, state.dataList);
            },
            separatorBuilder: (BuildContext context, int index) {
              return Container(height: 10);
            },
          ));
    });
  }

  buildProfileItem(index, List<dynamic> dataList) {
    CustomerListItem item = dataList[index];
    double height = 130;
    if((item.auditOpinion??"").isNotEmpty){
      height = 180;
    }
    return GestureDetector(
        onTap: () {
          var args = {"pageData": item};
          var result = HDNavigator.openToName(RouteConfig.customerDetail,
              arguments: args);
          if (result != null && result == "refresh") {
            state.controller.callRefresh();
          }
        },
        child: Card(
          margin: EdgeInsets.fromLTRB(20, 5, 20, 0),
          color: HDColors.white,
          shadowColor: Colors.amber,
          elevation: 1,
          shape: RoundedRectangleBorder(
              side: BorderSide(width: 0.3, color: HDColors.divider),
              borderRadius: BorderRadius.all(new Radius.circular(5))),
          child: Container(
              width: double.infinity,
              height: height,
              child: Stack(children: [
                Container(
                    margin: EdgeInsets.fromLTRB(20, 10, 20, 0),
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Icon(Icons.person),
                              Expanded(
                                  child: Text(item.customerName ?? "",
                                      style: const TextStyle(
                                          fontSize:
                                              StyleConfig.fontContentSize16,
                                          color: HDColors.black))),
                              CommonWidget.buildIconWidget(
                                  showIcon: false, nodestatue: "")
                            ],
                          ),
                          Container(
                              margin: EdgeInsets.fromLTRB(0, 5, 0, 10),
                              child: CommonWidget.divider(0.5)),
                          Row(
                            children: [
                              Icon(Icons.access_time, size: 17),
                              SizedBox(
                                width: 5,
                              ),
                              Text(item.updateDate ?? "",
                                  style: TextStyle(
                                      fontSize: StyleConfig.fontContentSize14,
                                      color: HDColors.siGrayHint))
                            ],
                          ),
                          SizedBox(height: 5),
                          Row(
                            children: [
                              Icon(Icons.phone, size: 17),
                              SizedBox(
                                width: 5,
                              ),
                              Text(item.phoneNumber ?? "",
                                  style: TextStyle(
                                      fontSize: StyleConfig.fontContentSize14,
                                      color: HDColors.siGrayHint))
                            ],
                          ),
                          SizedBox(height: 5),
                          Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Icon(
                                    color: HDColors.blue,
                                    Icons.location_on_outlined,
                                    size: 17),
                                SizedBox(
                                  width: 5,
                                ),
                                Expanded(
                                    child: Text(item.buildingAddress ?? "未知",
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                        style: TextStyle(
                                            fontSize:
                                                StyleConfig.fontContentSize14,
                                            color: HDColors.siGrayHint))),
                              ]),
                          SizedBox(height: 5),
                          Visibility(
                              visible: item.status == 22,
                              child: CommonWidget.buildTag(2)),
                          SizedBox(height: 5),
                          Visibility(
                              visible: (item.auditOpinion ?? "").isNotEmpty,
                              child: Text("原因：${item.auditOpinion}",
                                  maxLines: 2,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                      fontSize: StyleConfig.fontContentSize14,
                                      color: HDColors.siGrayHint)))
                        ])),
                Positioned(
                  right: 20,
                  top: 50,
                  child: Icon(Icons.arrow_forward_ios_sharp, size: 20),
                ),
              ])),
        ));
  }
}
